package com.bjtu.boatmq.common.protocol;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * 通信协议-包格式
 * @author cmy
 */
public class PacketSource {
    private static AtomicInteger atomicInteger = new AtomicInteger(0);
    /* header data */
    private int code;           // 请求/响应的类型代码，对应RequestCode/ResponseCode类
    private int requestId;      // 请求的唯一标识ID
    private int flag;           // 标识符：请求REQUEST（flag=0）和响应RESPONSE（flag=1）
    private int sponsorType;    // 发起者类型：BROKER(type=0), PRODUCER(type=1),CONSUMER(type=2)
    private String sponsorId;   // 发起者ID（string型，可能含有字母标识）
    private String remark;      // 其他备注信息（比如响应的详细错误信息/生产存储的偏移量）

    /* body data 具体的数据，比如消息、提交的偏移量、PULL请求的分区/消费偏移量 */
    private Object data;

    public PacketSource(){
        // 生成请求ID
        requestId = atomicInteger.getAndIncrement();
    }

    /* 静态成员变量，给flag/sponsorType赋值时使用 */
    public static final int REQUEST = 0;
    public static final int RESPONSE = 1;
    public static final int BROKER = 0;
    public static final int PRODUCER = 1;
    public static final int CONSUMER = 2;

    /* getter and setter函数 */
    public static AtomicInteger getAtomicInteger() {
        return atomicInteger;
    }

    public static void setAtomicInteger(AtomicInteger atomicInteger) {
        PacketSource.atomicInteger = atomicInteger;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public int getRequestId() {
        return requestId;
    }

    public void setRequestId(int requestId) {
        this.requestId = requestId;
    }

    public int getFlag() {
        return flag;
    }

    public void setFlag(int flag) {
        this.flag = flag;
    }

    public int getSponsorType() {
        return sponsorType;
    }

    public void setSponsorType(int sponsorType) {
        this.sponsorType = sponsorType;
    }

    public String getSponsorId() {
        return sponsorId;
    }

    public void setSponsorId(String sponsorId) {
        this.sponsorId = sponsorId;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}
